home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / dev / c / flash-0.4.3.lha / flash-0.4.3 / Lib / text.cc < prev    next >
C/C++ Source or Header  |  1999-02-21  |  5KB  |  246 lines

  1. /////////////////////////////////////////////////////////////
  2. // Flash Plugin and Player
  3. // Copyright (C) 1998,1999 Olivier Debon
  4. // 
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU General Public License
  7. // as published by the Free Software Foundation; either version 2
  8. // of the License, or (at your option) any later version.
  9. // 
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14. // 
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18. // 
  19. ///////////////////////////////////////////////////////////////
  20. //  Author : Olivier Debon  <odebon@club-internet.fr>
  21. //
  22.  
  23. #include <assert.h>
  24. #include "graphic.h"
  25. #include "text.h"
  26.  
  27. static char *rcsid = "$Id: text.cc,v 1.9 1999/01/31 20:31:10 olivier Exp $";
  28.  
  29. Text::Text(long id) : Character(TextType, id)
  30. {
  31.     textRecords = 0;
  32. }
  33.  
  34. Text::~Text()
  35. {
  36.     TextRecord     *cur,*del;
  37.  
  38.     for(cur = textRecords; cur;)
  39.     {
  40.         del = cur;
  41.         cur = cur->next;
  42.         delete del;
  43.     }
  44. }
  45.  
  46. void
  47. Text::setTextBoundary(Rect rect)
  48. {
  49.     boundary = rect;
  50. }
  51.  
  52. void
  53. Text::setTextMatrix(Matrix m)
  54. {
  55.     textMatrix = m;
  56. }
  57.  
  58. void
  59. Text::addTextRecord(TextRecord *tr)
  60. {
  61.     SwfFont *font = 0;
  62.     long n;
  63.  
  64.     tr->next = 0;
  65.  
  66.     if (textRecords == 0) {
  67.         textRecords = tr;
  68.         font = tr->font;
  69.     } else {
  70.         TextRecord *current;
  71.         long fontHeight;
  72.  
  73.         for(current = textRecords; current->next; current = current->next) {
  74.             if (current->flags & textHasFont) {
  75.                 font = current->font;
  76.                 fontHeight = current->fontHeight;
  77.             }
  78.         }
  79.  
  80.         current->next = tr;
  81.         if (current->flags & textHasFont) {
  82.             font = current->font;
  83.             fontHeight = current->fontHeight;
  84.         }
  85.  
  86.         if (tr->flags & textHasFont) {
  87.             font = tr->font;
  88.         } else {
  89.             tr->font = font;
  90.             tr->fontHeight = fontHeight;
  91.         }
  92.     }
  93.  
  94.     if (tr->nbGlyphs) {
  95.         for(n=0; n < tr->nbGlyphs; n++) {
  96.             tr->glyphs[n].code = font->getGlyphCode(tr->glyphs[n].index);
  97.         }
  98.     }
  99. }
  100.  
  101. int
  102. Text::execute(GraphicDevice *gd, Matrix *matrix, Cxform *cxform)
  103. {
  104.     return doText(gd, matrix, cxform, ShapeDraw, 0);
  105. }
  106.  
  107. void
  108. Text::getRegion(GraphicDevice *gd, Matrix *m, unsigned char id)
  109. {
  110.     doText(gd, m, 0, ShapeGetRegion, id);
  111. }
  112.  
  113. Rect
  114. Text::getBoundingBox()
  115. {
  116.     return boundary;
  117. }
  118.  
  119. TextRecord *
  120. Text::getTextRecords()
  121. {
  122.     return textRecords;
  123. }
  124.  
  125. int
  126. Text::doText(GraphicDevice *gd, Matrix *matrix, Cxform *cxform, ShapeAction action, unsigned char id)
  127. {
  128.     TextRecord    *tr;
  129.     long         x,y;        // Current position
  130.     SwfFont        *font = 0;    // Current font
  131.     long         fontHeight;
  132.     Matrix         tmat,fmat;
  133.     long         g;
  134.  
  135.     x = y = 0;
  136.     fontHeight = 0;
  137.  
  138.     // Compute final text matrix
  139.     tmat = (*matrix) * textMatrix;
  140.  
  141.     for(tr = textRecords; tr; tr = tr ->next)
  142.     {
  143.         if (tr->flags & isTextControl) {
  144.             if (tr->flags & textHasXOffset) {
  145.                 x = tr->xOffset;
  146.             }
  147.             if (tr->flags & textHasYOffset) {
  148.                 y = tr->yOffset;
  149.             }
  150.             if (tr->flags & textHasColor) {
  151.                 if (action == ShapeDraw) {
  152.                     if (cxform) {
  153.                         gd->setForegroundColor(cxform->getColor(tr->color));
  154.                     } else {
  155.                         gd->setForegroundColor(tr->color);
  156.                     }
  157.                 }
  158.             }
  159.         }
  160.  
  161.         font = tr->font;
  162.         fontHeight = tr->fontHeight;
  163.         // Update font matrix
  164.         fmat.a = fontHeight/1000.0;
  165.         fmat.d = fontHeight/1000.0;
  166.  
  167.         assert(font != 0);
  168.         for (g = 0; g < tr->nbGlyphs; g++)
  169.         {
  170.             Shape *shape;
  171.             Matrix cmat;
  172.  
  173.             shape = font->getGlyph( tr->glyphs[g].index );
  174.  
  175. #ifdef PRINT
  176.             printf("%c", font->getGlyphCode(tr->glyphs[g].index));
  177. #endif
  178.  
  179.             // Update font matrix
  180.             fmat.tx = x;
  181.             fmat.ty = y;
  182.  
  183.             // Compute Character matrix
  184.             cmat = tmat * fmat;
  185.  
  186.             if (action == ShapeDraw) {
  187.                 shape->execute(gd, &cmat, cxform);
  188.             } else {
  189.                 shape->doShape(gd, &cmat, cxform, action, id);
  190.             }
  191.  
  192.             // Advance
  193.             x += tr->glyphs[g].xAdvance;
  194.         }
  195. #ifdef PRINT
  196.         printf("\n");
  197. #endif
  198.     }
  199.  
  200.     if (gd->showMore) {
  201.         tmat = (*gd->adjust) * (*matrix);
  202.  
  203.         long x1,x2,y1,y2;
  204.  
  205.         x1 = boundary.xmin;
  206.         y1 = boundary.ymin;
  207.         x2 = boundary.xmax;
  208.         y2 = boundary.ymax;
  209.         gd->drawLine(tmat.getX(x1,y1),tmat.getY(x1,y1),tmat.getX(x2,y1),tmat.getY(x2,y1),20);
  210.         gd->drawLine(tmat.getX(x2,y1),tmat.getY(x2,y1),tmat.getX(x2,y2),tmat.getY(x2,y2),20);
  211.         gd->drawLine(tmat.getX(x2,y2),tmat.getY(x2,y2),tmat.getX(x1,y2),tmat.getY(x1,y2),20);
  212.         gd->drawLine(tmat.getX(x1,y2),tmat.getY(x1,y2),tmat.getX(x1,y1),tmat.getY(x1,y1),20);
  213.     }
  214.  
  215.     gd->synchronize();
  216.     return 0;
  217. }
  218.  
  219. ////////// TextRecord Methods
  220. TextRecord::TextRecord() {
  221.     flags = (TextFlags)0;
  222.     font = 0;
  223.     fontHeight = 0;
  224.     nbGlyphs = 0;
  225.     glyphs = 0;
  226.     xOffset = 0;
  227.     yOffset = 0;
  228. }
  229.  
  230. TextRecord::~TextRecord() {
  231.     if (nbGlyphs) delete glyphs;
  232. }
  233.  
  234. char *
  235. TextRecord::getText() {
  236.     static char text[256];
  237.     long g;
  238.  
  239.     for(g=0; g < nbGlyphs; g++) {
  240.         text[g] = glyphs[g].code;
  241.     }
  242.     text[g] = 0;
  243.  
  244.     return text;
  245. }
  246.